using System.Reflection;
using KinonekoSoftware.CodariaDB.Documents.Templates;

namespace KinonekoSoftware.Studio.Pages.Theories
{
    partial class WorldViewConfigViewModel
    {
        private InstanceTemplate _selectedInstanceTemplate;


        private void InitializeInstanceTemplates()
        {
            InstanceTemplates.AddMany(_engine_template.GetInstanceTemplates(), true);
            SelectedInstanceTemplate = InstanceTemplates.FirstOrDefault();
        }

        private void SaveTemplates()
        {

        }

        private async void DoAddInstanceTemplateCommand()
        {
            var r = await NewInstanceTemplateViewModel.New();

            if (!r.IsFinished)
            {
                return;
            }

            var target = r.Value;

            if (target.UseCustomType)
            {
                CustomTypeInstanceTemplates.Add(target);
            }
            else
            {
                var inside = _engine_template.HasInstanceTemplate(target.BuiltinType);

                if (inside is not null && !await WarningOperation(TitleSR.WarningOperation, "已经创建了该类型的设定模板，要替换现有的模板吗？"))
                {
                    return;
                }

                //
                //
                InstanceTemplates.Remove(target);
                _engine_template.Remove(target);
            }

            SelectedInstanceTemplate = target;
            InstanceTemplates.Add(target);
            _engine_template.Add(target);

            //
            // 设置编辑状态
            SetDirtyState(true);
        }

        private void DoReGenerateInstanceTemplateCommand()
        {
            var te = Platform.GetEngine<TemplateEngine>();

            //
            //
            te.ReGenerate();

            //
            // 重新初始化一遍
            InitializeInstanceTemplates();
        }

        private void DoEditInstanceTemplateDefinitionCommand(InstanceTemplate target)
        {
            if (target is null)
            {
                return;
            }
        }


        private async void DoEditInstanceTemplateCommand(InstanceTemplate target)
        {

            if (target is null)
            {
                return;
            }

            var r = await WVCTemplateDefinitionEditorViewModel.Edit(this, target);

            if (!r)
            {
                return;
            }

            _engine_template.UpdateInstanceTemplate(target);
            SetDirtyState(true);
        }


        private void DoExportInstanceTemplateCommand(InstanceTemplate target)
        {
            if (target is null)
            {
                return;
            }
        }


        private void DoImportInstanceTemplateCommand(InstanceTemplate target)
        {
            if (target is null)
            {
                return;
            }
        }

        private async void DoRemoveInstanceTemplateCommand(InstanceTemplate target)
        {
            if (target is null)
            {
                return;
            }


            if (InstanceTemplates.Count < 2)
            {
                await Warning(TitleSR.WarningOperation, TextSR.Remove_Item_Failed_CannotRemoveAll);
                return;
            }

            if (!await RemoveItem())
            {
                return;
            }

            if (ReferenceEquals(SelectedInstanceTemplate, target))
            {
                SelectedInstanceTemplate = null;
            }

            InstanceTemplates.Remove(target);
            _engine_template.Remove(target);

            //
            // 设置编辑状态
            SetDirtyState(true);
        }


        /// <summary>
        /// 获取或设置 <see cref="SelectedInstanceTemplate"/> 属性。
        /// </summary>
        public InstanceTemplate SelectedInstanceTemplate
        {
            get => _selectedInstanceTemplate;
            set => SetValue(ref _selectedInstanceTemplate, value);
        }

        public ObservableCollection<InstanceTemplate> InstanceTemplates           { get; }
        public ObservableCollection<InstanceTemplate> CustomTypeInstanceTemplates { get; }

        public ICommandEX AddInstanceTemplateCommand        { get; }
        public ICommandEX EditInstanceTemplateCommand       { get; }
        public ICommandEX ReGenerateInstanceTemplateCommand { get; }
        public ICommandEX ExportInstanceTemplateCommand     { get; }
        public ICommandEX ImportInstanceTemplateCommand     { get; }
        public ICommandEX RemoveInstanceTemplateCommand     { get; }
    }
}